import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:openim_common/openim_common.dart';

import 'friend_list_logic.dart';

class FriendListPage extends StatelessWidget {
  final logic = Get.find<FriendListLogic>();

  FriendListPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: TitleBar(
          height: 44.h,
          backgroundColor: Styles.c_FFFFFF,
          center: Expanded(
            child: Center(
              child: StrRes.contacts.toText..style = Styles.ts_0C1C33_20sp_semibold,
            ),
          ),
          right: GestureDetector(
            onTap: () {
              // 点击搜索图标的处理（暂时为空）
              logic.search();
            },
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 8.w),
              child: ImageRes.searchBlack.toImage
                ..width = 28.w
                ..height = 28.h,
            ),
          ),
      ),
      backgroundColor: Styles.c_F8F9FA,
      body: Column(
        children: [
              // 新朋友项目
          _buildFixedItemView(
            assetsName: ImageRes.newFriend,
            label: StrRes.newFriend,
            count: logic.friendApplicationCount,
            onTap: () => logic.newFriend(),
          ),
          // 关注通知项目
          _buildFixedItemView(
            assetsName: ImageRes.newNotify,
            label: StrRes.newNotify,
            count: logic.groupApplicationCount,
            onTap: () => logic.newGroup(),
          ),
          // 群聊项目
          _buildFixedItemView(
            assetsName: ImageRes.myGroup,
            label: StrRes.myGroup,
            onTap: () => logic.myGroup(),
          ),
          // 分隔线
          Container(
            height: 8.h,
            color: Styles.c_F8F9FA,
          ),
          Flexible(
            child: Obx(
              () => WrapAzListView<ISUserInfo>(
                data: logic.friendList,
                itemCount: logic.friendList.length,
                itemBuilder: (_, data, index) => _buildItemView(data),
              ),
            ),
          ),
        ],
      ),
    );
  }
  Widget _buildFixedItemView({
    String? assetsName,
    required String label,
    Widget? icon,
    int count = 0,
    bool showRightArrow = true,
    double? height,
    Function()? onTap,
    Color? backgroundColor,
  }) =>
      Ink(
        color: backgroundColor ?? Styles.c_FFFFFF, // 使用自定义背景色或默认白色
        child: InkWell(
          onTap: onTap,
          child: Container(
            height: height ?? 60.h,
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Row(
              children: [
                if (null != assetsName)
                  assetsName.toImage
                    ..width = 42.w
                    ..height = 42.h,
                if (null != icon) icon,
                12.horizontalSpace,
                label.toText..style = Styles.ts_0C1C33_17sp,
                const Spacer(),
                if (count > 0) UnreadCountView(count: count),
                4.horizontalSpace,
                if (showRightArrow)
                  ImageRes.rightArrow.toImage
                    ..width = 24.w
                    ..height = 24.h,
              ],
            ),
          ),
        ),
      );
  Widget _buildItemView(ISUserInfo info) => Ink(
        height: 64.h,
        color: Styles.c_FFFFFF,
        child: InkWell(
          onTap: () => logic.viewFriendInfo(info),
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Row(
              children: [
                AvatarView(
                  url: info.faceURL,
                  text: info.showName,
                ),
                10.horizontalSpace,
                info.showName.toText..style = Styles.ts_0C1C33_17sp,
              ],
            ),
          ),
        ),
      );
}
